package de.geraldschenke.tictactoe.common.ai;

import android.util.Log;
import de.geraldschenke.tictactoe.common.GameEvent;
import de.geraldschenke.tictactoe.common.PlayerEvent;
import de.geraldschenke.tictactoe.common.RemotePlayer;
import de.geraldschenke.tictactoe.server.GameThread;

public class StupidAI implements RemotePlayer {

	private GameThread gameThread;
	private int playerId = -1;

	public StupidAI(GameThread gameThread) {
		this.gameThread = gameThread;
	}

	@Override
	public void handleGameEvent(GameEvent gameEvent) {
		if (gameEvent.gameOver || gameEvent.currentPlayer != playerId) {
			Log.d("StupidAI", "got Event, but not my turn or Game Over");
			return;
		}

		for(int i = 0; i < gameEvent.board.length; i++){
			for(int j = 0; j < gameEvent.board[i].length; j++){
				if (gameEvent.board[i][j] == null){
					PlayerEvent playerEvent = new PlayerEvent();
					playerEvent.type = PlayerEvent.Type.SET;
					playerEvent.x = i;
					playerEvent.y = j;
					gameThread.handlePlayerEvent(playerEvent);
					return;
				}
			}
		}
	}

	@Override
	public void setPlayerId(int id) {
		playerId = id;
	}
}
